package com.xnfh.planting.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.xnfh.common.bean.PlantOptionBean
import com.xnfh.common.bean.PlantOptionBeanConvert
import com.xnfh.common.bean.Record
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.planting.R
import com.xnfh.planting.databinding.FragmentPlantOptionBinding
import com.xnfh.planting.ui.adapter.PlantOptionAdapter
import com.xnfh.planting.viewmodel.PlantOptionViewModel

/**
 * @Description:种植方案
 * @Author: ziheng
 * @CreateDate: 2023/12/15 15:37
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/15 15:37
 * @UpdateRemark: 更新说明
 */
class PlantOptionFragment : BaseFragment<FragmentPlantOptionBinding>() {
    override fun getLayoutID(): Int = R.layout.fragment_plant_option

    companion object {
        fun getInstance(bundle: Bundle? = null): PlantOptionFragment {
            val plantOptionFragment = PlantOptionFragment()
            if (bundle != null) {
                plantOptionFragment.arguments = bundle
            }
            return plantOptionFragment
        }
    }

    private var plantOptionViewModel: PlantOptionViewModel? = null

    //传入的地块信息
    private var fieldDetailBean: Record? = null
    private var adapter: PlantOptionAdapter? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        fieldDetailBean = arguments?.getSerializable("data") as Record
        mBind.plantOptionRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = PlantOptionAdapter()
        mBind.plantOptionRecycle.adapter = adapter

        //请求数据
        plantOptionViewModel?.requestPlantOptionList(
            fieldDetailBean?.provinceId,
            fieldDetailBean?.cityId,
            fieldDetailBean?.startTime,
            fieldDetailBean?.plantCropId
        )

        adapter?.setPlantOptionCallBack(object : PlantOptionAdapter.PlantOptionCallBack {
            override fun XunTianOnClick(isDisease: Boolean, item: PlantOptionBeanConvert) {

            }

            override fun IntServDetailOnClick(item: PlantOptionBeanConvert) {

            }
        })
    }

    override fun observe() {
        plantOptionViewModel = PlantOptionViewModel()
        plantOptionViewModel?.plantOptionList?.observe(this,
            object : BaseStateObserver<List<PlantOptionBean>>(null) {
                override fun getRespDataSuccess(it: List<PlantOptionBean>) {
                    super.getRespDataSuccess(it)
                    if (it.isEmpty()) {
                        adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                        return
                    }
                    //这里处理数据转换，等后端优化之后重新做逻辑
                    val plantOptionBeanConverts = ArrayList<PlantOptionBeanConvert>()
                    for ((index, plantOptionBean) in it.withIndex()) {
                        for ((i, movent) in it[index].movents.withIndex()) {
                            plantOptionBeanConverts.add(
                                PlantOptionBeanConvert(
                                    plantOptionBean.startTime,
                                    plantOptionBean.endTime,
                                    plantOptionBean.movents[i].standard,
                                    plantOptionBean.movents[i].name,
                                    plantOptionBean.movents[i].ai,
                                    plantOptionBean.movents[i].id,
                                    plantOptionBean.movents[i].describe,
                                    plantOptionBean.growthName,
                                    plantOptionBean.movents[i].type
                                )
                            )
                        }
                    }
                    adapter?.setNewInstance(plantOptionBeanConverts)
                }
            })
    }
}